Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/libusb #242

Merged
merged 15 commits into from
Jan 12, 2020
Merged

Feature/libusb #242

merged 15 commits into from
Jan 12, 2020

Conversation

magnusja
Copy link
Owner

No description provided.

@codecov
Copy link

codecov bot commented Jan 10, 2020

Codecov Report

Merging #242 into develop will decrease coverage by 0.06%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           develop    #242      +/-   ##
==========================================
- Coverage    62.86%   62.8%   -0.07%     
==========================================
  Files           49      49              
  Lines         1570    1570              
  Branches       216     217       +1     
==========================================
- Hits           987     986       -1     
- Misses         518     519       +1     
  Partials        65      65
Impacted Files Coverage Δ
...a/com/github/mjdev/libaums/fs/FileSystemFactory.kt 100% <ø> (ø) ⬆️
...a/com/github/mjdev/libaums/usb/UsbCommunication.kt 0% <ø> (ø) ⬆️
...ithub/mjdev/libaums/usb/UsbCommunicationFactory.kt 0% <0%> (ø) ⬆️
...ithub/mjdev/libaums/usb/AndroidUsbCommunication.kt 0% <0%> (ø) ⬆️
...ithub/mjdev/libaums/driver/scsi/ScsiBlockDevice.kt 0% <0%> (ø) ⬆️
...m/github/mjdev/libaums/fs/fat32/FsInfoStructure.kt 76.19% <0%> (-4.77%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 909b79e...ef76d6f. Read the comment docs.

Comment on lines 38 to 45
// val ret = nativeClaimInterface(libUsbHandle, usbInterface.id)
// if (ret < 0) {
// throw IOException("libusb returned $ret in claim interface")
// }
val claim = deviceConnection!!.claimInterface(usbInterface, true)
if (!claim) {
throw IOException("could not claim interface!")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't libusb claim it? I'm worried this may cause issues because I've seen it internally does unclaim/claim in reset_device, for example.

@magnusja
Copy link
Owner Author

magnusja commented Jan 10, 2020 via email

@depau
Copy link
Collaborator

depau commented Jan 11, 2020

Yes, I figured listing devices wouldn't work, but I didn't expect the claiming process would require assistance from the system. I didn't get the chance to test :/ maybe within a few days

@magnusja
Copy link
Owner Author

I did not put too much time into that, so maybe we are still missing something to claim the interface using libusb. I guess one easy test would be to try claiming with a rooted device to see if it is really a permission problem.

@magnusja
Copy link
Owner Author

I think internally it tries to claim/unclaim sometimes but the code I have seen does usually ignore errors from claiming and just proceeds.

// val ret = nativeClaimInterface(libUsbHandle, usbInterface.id)
// if (ret < 0) {
// throw IOException("libusb returned $ret in claim interface")
// }
val claim = deviceConnection!!.claimInterface(usbInterface, true)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@depau so this seems to work

@magnusja
Copy link
Owner Author

I made some tests with a usb hub and two drives, which usually never worked. With libusb this works quite well, so I hope that you will come to the same conclusion :)
(Although I have not seen the reset procedure, I think)

}
}

interface UsbCommunicationCreator {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* Created by magnusja on 21/12/16.
*/

object UsbCommunicationFactory {
class NoUsbCommunicationFound : IOException()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private external fun nativeClose(handle: Long, interfaceNumber: Int)
private external fun nativeReset(handle: Long)
private external fun nativeClearHalt(handle: Long, interfaceNumber: Int)
private external fun nativeBulkTransfer(handle: Long, endpointAddress: Int, data: ByteArray, offset: Int, length: Int, timeout: Int): Int
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import java.nio.ByteBuffer
import kotlin.math.absoluteValue

class LibusbCommunication(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


}

class LibusbCommunicationCreator : UsbCommunicationCreator {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private external fun nativeReset(handle: Long)
private external fun nativeClearHalt(handle: Long, interfaceNumber: Int)
private external fun nativeBulkTransfer(handle: Long, endpointAddress: Int, data: ByteArray, offset: Int, length: Int, timeout: Int): Int
private external fun nativeControlTransfer(handle: Long, requestType: Int, request: Int, value: Int, index: Int, buffer: ByteArray, length: Int, timeout: Int): Int
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import java.nio.ByteBuffer
import kotlin.math.absoluteValue

class LibusbCommunication(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

interface UsbCommunicationCreator {
fun create(usbManager: UsbManager,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@depau
Copy link
Collaborator

depau commented Jan 12, 2020

I'm testing it and it's doing great! I'll see how it does on QEMU if my tiny RAM allows it

@depau
Copy link
Collaborator

depau commented Jan 12, 2020

I tested it on my two phones and on QEMU with a real USB drive passed through, it's working much better than the Android API implementation.

Still not working on QEMU with an emulated drive, it times out: https://pastebin.com/iGxQCz0k

I'm still not aware of what's wrong with it, it works fine if I pass-through a real USB drive, the emulated one works with Linux but not in userspace.

@magnusja
Copy link
Owner Author

Hey @depau

thanks a lot for testing! Awesome, that is good news. Then I will merge this and prepare a release so that other people can test this as well.

Regarding QEMU: Well that is unfortunate. If the emulated drive does not work in userspace in Linux the same problem might be in Android then as well. Can you maybe provide me with information on running QEMU, so that I can play around with this as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants